package com.iaz.tech.tools.property.project;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iaz.tech.tools.common.utils.UtilTools;
import com.iaz.tech.tools.message.IMessageAble;
import com.iaz.tech.tools.property.IPropertyAble;
import com.iaz.tech.tools.property.impl.Property;
import com.iaz.tech.tools.property.impl.PropertyGroup;
import com.iaz.tech.tools.property.impl.ValueProperty;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * project 任务包，用于规划复杂
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class Project extends Property {
	
	/**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(Project.class);
	
	private String projectID = "";
	
	/**
	 * 子工程
	 */
	private List<Project> childrenProject = new ArrayList<Project>();
	/**
	 * 工程信息，（属性）文件	
	 */
	private IPropertyAble properties;

	private static Integer _INNER_PROJECT_SERIALNO = 0;
	private final static String __SYN_PROJECT_SERAILNO = "_N_LOCK_FOR_PROJECT_SERIAL";
	/**
	 * 工程是否结束
	 */
	private boolean isEnd = false;

	private static String generateProjectID() {
		String result = "PROJ_";
		try {
			synchronized(__SYN_PROJECT_SERAILNO){
				if (_INNER_PROJECT_SERIALNO > 999) {
					_INNER_PROJECT_SERIALNO = 0;
				}
				_INNER_PROJECT_SERIALNO++;
			}
			
			result = result + UtilTools.getCurrentTime() + "_" + _INNER_PROJECT_SERIALNO;
		} catch(Exception e) {
			logger.error("generateProjectID error: {}", e.getMessage());
			result = "PROJ_";
		}
		return result;
	}
	
	/**
	 * 程序体，用于生成
	 */
	private IPropertyAble projectResult;

	public Project() {
		super();
		this.projectID = generateProjectID();
	}

	/**
	 * 消息
	 * @param msg
	 */
	public void message(IMessageAble msg) {
		try {
			if (msg == null) {
				throw new Exception("msg 为空！");
			}
			
			
			
		} catch(Exception e) {
			logger.error("异常：" + e.getMessage());
		}
	}
	/**
	 * 获得工程所属基础Path
	 * @return
	 */
	public String getBaseProjectPath() {
		String result = "";
		try {
			if (this.properties == null) {
				throw new Exception("properties 为空！");
			}
			IPropertyAble propable = ((PropertyGroup)this.properties).getProperty("BASE_PROJECT_PATH");
			if (propable == null) {
				throw new Exception("BASE_PROJECT_PATH 为空！");
			}
			if (propable instanceof ValueProperty) {
				result = (String)((ValueProperty)propable).toString();
			}
		} catch(Exception e) {
			logger.error("getBaseProjectPath error: {}", e.getMessage());
		}
		return result;
	}

}
